package icu.shaoyayu.common.utll;


import icu.shaoyayu.common.utll.exception.CustomExceptionType;

/**
 * @author shaoyayu
 * @date 2022/2/9
 * @apiNote
 */
public class ReturnUtil {
    /**
     * 成功的返回
     * @param <T> 不需要传递参数
     * @return
     */
    public static<T> R success(){
        return success("请求响应成功",null,null);
    }

    /**
     * 自定义返回的消息内容
     * @param msg
     * @param <T>
     * @return
     */
    public static<T> R success(String msg){
        return success(msg,null,null);
    }

    /**
     *
     * @param data
     * @param <T>
     * @return
     */
    public static<T> R success(T data){
        return success("请求响应成功",data,null);
    }

    /**
     *
     * @param data
     * @param count
     * @param <T>
     * @return
     */
    public static<T> R success(T data, Integer count){
        return success("请求响应成功",data,count);
    }

    /**
     *
     * @param msg
     * @param data
     * @param count
     * @param <T>
     * @return
     */
    public static<T> R success(String msg, T data, Integer count){
        R<T> r = new R<>();
        r.setOk(true);
        r.setCode(200);
        r.setMsg(msg);
        r.setData(data);
        r.setCount(count);
        r.setTimestamp(System.currentTimeMillis());
        return  r;
    }

    public static<T> R error(CustomExceptionType exceptionType){
        return error(exceptionType.getCode(),exceptionType.getDesc());
    }

    /**
     * 自定义错误返回
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static<T> R error(Integer code, String msg){
        R<T> r = new R<>();
        r.setOk(false);
        r.setCode(code);
        r.setMsg(msg);
        r.setData(null);
        r.setCount(null);
        r.setTimestamp(System.currentTimeMillis());
        return r;
    }

    public static<String> R error(CustomExceptionType exceptionType, String body){
        R<String> r = new R<>();
        r.setOk(false);
        r.setCode(exceptionType.getCode());
        r.setMsg(exceptionType.getDesc());
        r.setData(body);
        r.setCount(null);
        r.setTimestamp(System.currentTimeMillis());
        return r;
    }
}
